package com.efounder.frame.utils;

import com.efounder.constant.EnvironmentVariable;
import com.efounder.form.application.ApplicationContainer;
import com.efounder.v8.ConcurrentV8;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import static com.efounder.frame.utils.Constants.CHAT_USER_ID;

/**
 * Created by XinQing on 2016/9/10.
 */

public class EFAppAccountUtils {

//    public static ApplicationContainer applicationContainer;
    public static Map<String,ApplicationContainer> applicationContainerMap = new HashMap<>();
    public static ConcurrentV8 V8;

    public static void saveAppAccountID(int appAccountID){
        EnvironmentVariable.setProperty("app_account_id",appAccountID + "");
    }
    public static void saveAppAccountName(String appAccountName){
        EnvironmentVariable.setProperty("app_account_name",appAccountName);
    }

    public static String getAppAccountName(){
        String name = EnvironmentVariable.getProperty("app_account_name","");
        return name;
    }
    public static int getAppAccountID(){
        String id = EnvironmentVariable.getProperty("app_account_id","0");
        return Integer.parseInt(id);
    }

    public static String getAppAccountUniqueTag(int appAccountID){
        String uniqueTag = "res_" + EnvironmentVariable.getProperty(CHAT_USER_ID) + "_" + appAccountID ;
        return uniqueTag;
    }
//    public static String getAppAccountUniqueTag(int appAccountID){
//        String uniqueTag = EnvironmentVariable.getProperty(CHAT_USER_ID) + appAccountID + "res";
//        return uniqueTag;
//    }

    public static String getNickName(){
        String nickName = EnvironmentVariable.getProperty(Constants.KEY_NICK_NAME,"未知");
        return nickName;
    }

    public static String getAppAccountAppsPath(){
        String path = null;
        path = EFConstants.APP_ROOT + "/" + EnvironmentVariable.getProperty(Constants.CHAT_USER_ID) + "/Apps";
        return path;
    }
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public static String getAppAccountRootPath(){
        return getAppAccountRootPath(getAppAccountID());
    }
    public static String getAppAccountRootPath(int appAccountID){
        String path = null;
        path = getAppAccountAppsPath() + "/" +appAccountID;
        return path;
    }

    public static String getAppAccountImagePath(){
        String path = getAppAccountRootPath() + "/image";
        return path;
    }
    public static String getAppAccountImagePath(int appAccountID){
        String path = getAppAccountRootPath(appAccountID) + "/image";
        return path;
    }
    public static String getAppAccountFormPath(){
        String path = getAppAccountRootPath() + "/form";
        return path;
    }
    public static String getAppAccountFormPath(int appAccountID){
        String path = getAppAccountRootPath(appAccountID) + "/form";
        return path;
    }
    public static String getAppAccountJsonPath(){
        String path = getAppAccountRootPath() + "/json";
        return path;
    }
    public static String getAppAccountJsonPath(int appAccountID){
        String path = getAppAccountRootPath(appAccountID) + "/json";
        return path;
    }
    public static String getAppAccountMDMPath(){
        String path = getAppAccountRootPath() + "/mdm";
        return path;
    }
    public static String getAppAccountMDMPath(int appAccountID){
        String path = getAppAccountRootPath(appAccountID) + "/mdm";
        return path;
    }
    public static String getAppAccountAppFramePath(){
        String path = getAppAccountRootPath() + "/app_frame.xml";
        return path;
    }
    public static String getAppAccountAppFramePath(int appAccountID){
        String path = getAppAccountRootPath(appAccountID) + "/app_frame.xml";
        return path;
    }
    public static String getAppAccountIconPath(){
        String path = getAppAccountImagePath() + "/icon";
        return path;
    }
    public static String getAppAccountIconPath(int appAccountID){
        String path = getAppAccountImagePath(appAccountID) + "/icon";
        return path;
    }

    public static void deleteAppAccountRoot(int appAccountID){
        String appAccountRootPath = getAppAccountRootPath(appAccountID);
        deleteAllFilesOfDir(new File(appAccountRootPath));
    }

    public static void deleteAllFilesOfDir(File path) {
        if (!path.exists())
            return;
        if (path.isFile()) {
            path.delete();
            return;
        }
        File[] files = path.listFiles();
        for (int i = 0; i < files.length; i++) {
            deleteAllFilesOfDir(files[i]);
        }
        path.delete();
    }
}
